package com.k2data.k2app.service;

import com.google.common.collect.Maps;
import com.k2data.k2app.domain.po.Problem;
import com.k2data.k2app.mapper.ProblemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by k2data on 18-1-18.
 */
@Service
public class ProblemService {

    private final ProblemMapper problemMapper;

    @Autowired
    public ProblemService(ProblemMapper problemMapper) {
        this.problemMapper = problemMapper;
    }

    public Problem getProblemById(long id){
        return problemMapper.selectById(id);
    }

    public Map<String,List<String>> getSelectList(){
        Map<String,List<String>> resultMap= Maps.newHashMap();

        List<String> vibrationDominantFrequencyL =problemMapper.selectVibrationDominantFrequency();
        removeNull(vibrationDominantFrequencyL);
        resultMap.put("vibrationDominantFrequency",vibrationDominantFrequencyL);

        List<String> vibrationDirectionL=problemMapper.selectVibrationDirection();
        removeNull(vibrationDirectionL);
        resultMap.put("vibrationDirection",vibrationDirectionL);

        List<String> withFrequencyL=problemMapper.selectWithFrequency();
        removeNull(withFrequencyL);
        resultMap.put("withFrequency",withFrequencyL);

        List<String> vibrationStabilityL =problemMapper.selectVibrationStability();
        removeNull(vibrationStabilityL);
        resultMap.put("vibrationStability",vibrationStabilityL);

        List<String> phaseCharacteristicsL=problemMapper.selectPhaseCharacteristics();
        removeNull(phaseCharacteristicsL);
        resultMap.put("phaseCharacteristics",phaseCharacteristicsL);

        List<String> vibrationLoadChangeL=problemMapper.selectVibrationLoadChange();
        removeNull(vibrationLoadChangeL);
        resultMap.put("vibrationLoadChange",vibrationLoadChangeL);

        List<String> vibrationSpeedChangeL=problemMapper.selectVibrationSpeedChange();
        removeNull(vibrationSpeedChangeL);
        resultMap.put("vibrationSpeedChange",vibrationSpeedChangeL);

        List<String> vibrationOilChangeL=problemMapper.selectVibrationOilChange();
        removeNull(vibrationOilChangeL);
        resultMap.put("vibrationOilChange",vibrationOilChangeL);

        List<String> vibrationPressureChangeL=problemMapper.selectVibrationPressureChange();
        removeNull(vibrationPressureChangeL);
        resultMap.put("vibrationPressureChange",vibrationPressureChangeL);

        List<String> rotorTrajectoryL=problemMapper.selectRotorTrajectory();
        removeNull(rotorTrajectoryL);
        resultMap.put("rotorTrajectory",rotorTrajectoryL);
        return resultMap;
    }

    //去除空值和空字符串
    private static <T> List<T> removeNull(List<? extends T> list) {
        list.removeAll(Collections.singleton(null));
        list.removeAll(Collections.singleton(""));
        return (List<T>) list;
    }

    public List<Problem> getProblems(Problem problem){
        return problemMapper.selectProblemList(problem);
    }

    public int delProblem(long id){
        return problemMapper.delProblem(id);
    }

    public int addProblem(Problem problem){
        return problemMapper.insertSelective(problem);
    }

    public int updateProblem(Problem problem){
        return problemMapper.updateByPrimaryKeySelective(problem);
    }
}
